package com.jiang.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.jiang.dao.INewsDAO;
import com.jiang.dao.impl.NewsDAOImpl;
import com.jiang.factory.DAOFactory;
import com.jiang.service.INewsService;
import com.jiang.utils.DBConnectionUtils;
import com.jiang.vo.News;

public class NewsServiceImpl implements INewsService {

	private DBConnectionUtils dbUtils = new DBConnectionUtils();

	@Override
	public boolean addNews(News news) throws Exception {
		try {
			INewsDAO newsDAO = DAOFactory.getInstance(NewsDAOImpl.class, dbUtils.getConnection());
			return newsDAO.doCreateNews(news);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			dbUtils.closeConnection();
		}

	}

	@Override
	public boolean updateNews(News news) throws Exception {

		try {
			INewsDAO newDAO = DAOFactory.getInstance(NewsDAOImpl.class, dbUtils.getConnection());
			return newDAO.doUpdate(news);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			dbUtils.closeConnection();
		}

	}

	@Override
	public boolean batchRemove(Set<Integer> nids) throws Exception {

		try {
			INewsDAO newDAO = DAOFactory.getInstance(NewsDAOImpl.class, dbUtils.getConnection());
			return newDAO.doRemoveBatch(nids);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			dbUtils.closeConnection();
		}
	}

	@Override
	public News findById(int nid) throws Exception {

		try {
			INewsDAO newDAO = DAOFactory.getInstance(NewsDAOImpl.class, dbUtils.getConnection());
			return newDAO.doFindById(nid);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			dbUtils.closeConnection();
		}

	}

	@Override
	public List<News> find(String column, String value) throws Exception {
		
		try{
			INewsDAO newDAO = DAOFactory.getInstance(NewsDAOImpl.class, dbUtils.getConnection());
			return newDAO.doFind(column, value);
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}finally{
			dbUtils.closeConnection();
		}
	}

	@Override
	public List<News> findAll() throws Exception {
		
		try{
			INewsDAO newDAO = DAOFactory.getInstance(NewsDAOImpl.class, dbUtils.getConnection());
			return newDAO.doFindAll();
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}finally{
			dbUtils.closeConnection();
		}
		
	}

	@Override
	public Map<String, Object> findSplit(String column, String value, int curPage, int lineSize) throws Exception {
		
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			INewsDAO newDAO = DAOFactory.getInstance(NewsDAOImpl.class, dbUtils.getConnection());
			List<News> newsList = newDAO.doSplit(column, value, curPage, lineSize);
			int countNum = newDAO.getCount(column, value);
			map.put("results", newsList);
			map.put("count", countNum);
			return map;
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}finally{
			dbUtils.closeConnection();
		}
	}

}
